package com.ssyt.blog.core.sys.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssyt.blog.core.common.PageModel;
import com.ssyt.blog.core.sys.entity.Article;
import com.ssyt.blog.core.sys.entity.Blog;
import com.ssyt.blog.core.sys.entity.Discuss;
import com.ssyt.blog.core.sys.entity.User;
import com.ssyt.blog.core.sys.service.ArticleService;
import com.ssyt.blog.core.sys.service.BlogService;
import com.ssyt.blog.core.sys.service.DiscussService;

@Controller
@RequestMapping("/sys")
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private DiscussService discussService;
	@Autowired
	private BlogService blogService;

	@RequestMapping("/addArticle")
	public String addArticle(Article article) throws Exception {
		article.setCreateTime(new Date());
		article.setReadTimes(0);
		articleService.addArticle(article);
		return "redirect:/views/blog/blog-list";
	}

	@ResponseBody
	@RequestMapping("/articleList")
	public Object articleList(int pageNo) throws Exception {
		PageModel<Article> pageModel = articleService.list(pageNo);
		return pageModel;
	}

	@RequestMapping("/getArticle")
	public String getArticle(int id) throws Exception {
		articleService.addReadTimes(id);
		return "redirect:readArticle.action?id=" + id;
	}

	@RequestMapping("/readArticle")
	public String readArticle(int id, Model model) throws Exception {
		Article article = articleService.getArticle(id);
		List<Discuss> discussList = discussService.discussListByArticle(id);
		model.addAttribute("article", article);
		model.addAttribute("discussList", discussList);
		return "views/blog/particulars";
	}

	@ResponseBody
	@RequestMapping("/addDiscuss")
	public Object addDiscuss(String content, int articleId, HttpSession session)
			throws Exception {
		User user = (User) session.getAttribute("user");
		int userId = user.getId();
		int discussId = discussService.addDiscuss(content, articleId, userId);
		Discuss discuss = new Discuss(discussId, content, new Date(),
				articleId, userId, user.getName(), user.getPhoto());
		return discuss;
	}

	@ResponseBody
	@RequestMapping("/allRankList")
	public Object allRankList() throws Exception {
		List<Article> articleList = articleService.allRankList();
		return articleList;
	}

	@ResponseBody
	@RequestMapping("/authorRankList")
	public Object authorRankList(int userId) throws Exception {
		List<Article> articleList = articleService.authorRankList(userId);
		return articleList;
	}

	@RequestMapping("/authorBlog")
	public String authorBlog(int userId, int pageNo, Model model)
			throws Exception {
		PageModel<Article> articlePage = articleService.getArticleList(userId,
				pageNo);
		model.addAttribute("articlePage", articlePage);
		Blog blog = blogService.getBlog(userId);
		model.addAttribute("blog", blog);
		return "views/blog/private";
	}

	@ResponseBody
	@RequestMapping("/categoryInfo")
	public Object categoryInfo(int userId) throws Exception {
		List<Object[]> articleList = articleService.categoryInfo(userId);
		return articleList;
	}

	@RequestMapping("/getArticleByCategory")
	public String getArticleByCategory(int categoryId, int userId, int pageNo,
			Model model) throws Exception {
		PageModel<Article> articlePage = articleService.getArticleByCategory(
				categoryId, userId, pageNo);
		model.addAttribute("articlePage", articlePage);
		Blog blog = blogService.getBlog(userId);
		model.addAttribute("blog", blog);
		return "views/blog/private";
	}
}
